//title 导航标题
//index 是否下面还有子菜单  0没有 1有
//icon  菜单列表的字体图标
//link  连接地址
//child 子菜单列表
////生成导航数据
console.log(window.location.href)
if(!window.sessionStorage.getItem("user")) {
	//	if(window.location.href == )
	//	console.log(window.location.href)
	window.location.href = "../login/login.html";
}

function topHeader() {
	var userAccount = JSON.parse(window.sessionStorage.getItem("user")).msg.userAccount
	var str = '<ul class="fl">' +
		'<li class="layui-nav-item">' +
		'<a href="javascript:;" style="font-size: 20px;">' +
		'LNG天然气管理系统</a>' +
		'</li>' +
		'</ul>' +
		'<ul class="fr" style="margin-right: 60px;">' +
		'<li class="layui-nav-item" lay-unselect="">' +
		'<a href="../index/home.html" target = "_blank">进入首页</a>' +
		'</li>' +
		'<li class="layui-nav-item" lay-unselect="">' +
		'<a href="javascript:;">你好，<span>' + userAccount + '</span></a>' +
		'</li>' +
		'<li class="layui-nav-item">' +
		'<a href="javascript:;" id="outLogin"><i class="iconfont icon-zhuxiaodenglu"></i>退出</a>' +
		'</li>' +
		'</ul>';
	var top = document.getElementById("headerTop");
	if(top) {
		$(top).append(str);
	} else {
		console.log("请初始化元素");
	}
}
topHeader();
$("#outLogin").on("click", function() {
	window.sessionStorage.clear();
	window.location.href = "../login/login.html";
});

function getClientHeight() {
	var clientHeight = 0;
	if(document.body.clientHeight && document.documentElement.clientHeight) {
		var clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
	} else {
		var clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
	}
	console.log(clientHeight)
	clientHeight = clientHeight - 60;
	$("#left_menu").height(clientHeight);
}
window.resize = function() {
	getClientHeight();
};
var navData = {
	leftNav: [{
			title: 'welcome',
			index: 0,
			icon: 'layui-icon-face-smile',
			link: '/wan/code/web/welcome/welcome.html?pos=6',
		},
//		{
//			title: '设备管理',
//			index: 1,
//			icon: 'iconfont icon-shebeiguanli',
//			link: '',
//			child: [{
//				title: '设备台账列表',
//				index: 0,
//				icon: 'iconfont icon-wuxupailie',
//				link: '/wan/code/web/accountsMagessTem/accountsList.html?pos=0&childPos=0',
//			}, {
//				title: '厂商列表',
//				index: 0,
//				icon: 'iconfont icon-wuxupailie',
//				link: '/wan/code/web/accountsMagessTem/accountsShopList.html?pos=0&childPos=1',
//				//					link:'../accountsShopList.html?pos=0&childPos=1'
//			}]
//		}, {
//			title: '站点管理',
//			index: 1,
//			icon: 'iconfont icon-guandao',
//			link: '',
//			child: [{
//				title: '站点列表',
//				index: 0,
//				icon: 'iconfont icon-wuxupailie',
//				link: '/wan/code/web/zhandian/zhandianList.html?pos=1&childPos=0',
//			}, {
//				title: '计量回路列表',
//				index: 0,
//				icon: 'iconfont icon-wuxupailie',
//				link: '/wan/code/web/zhandian/zhandianBack.html?pos=1&childPos=1',
//
//			}]
//		}, {
//			title: '数据监控',
//			index: 1,
//			icon: 'iconfont icon-fenxi',
//			link: '',
//			child: [{
//					title: '门店监控信息',
//					index: 0,
//					icon: 'iconfont icon-mendian',
//					link: '/wan/code/web/dataMonitorManage/storeMoniterList.html?pos=2&childPos=0'
//
//				}, {
//					title: 'LNG数据监控',
//					index: 0,
//					link: '/wan/code/web/dataMonitorManage/lngMoniterList.html?pos=2&childPos=1'
//				}, {
//					title: '调压站数据监控',
//					index: 0,
//					icon: 'iconfont icon-dianqi',
//					link: '/wan/code/web/dataMonitorManage/voltageMoniterList.html?pos=2&childPos=2'
//				}, {
//					title: '电厂数据监控',
//					index: 0,
//					icon: 'iconfont icon-dianqi',
//					link: '/wan/code/web/dataMonitorManage/plantMoniterList.html?pos=2&childPos=3'
//				}, {
//					title: '直属用户数据监控',
//					index: 0,
//					icon: 'iconfont icon-gerenxinxi',
//					link: '/wan/code/web/dataMonitorManage/directMoniterList.html?pos=2&childPos=4'
//				}, {
//					title: '次高压气量平衡报表',
//					index: 0,
//					icon: 'iconfont icon-wuxupailie',
//					link: '/wan/code/web/dataMonitorManage/secondHignReportList.html?pos=2&childPos=5'
//				}, {
//					title: '高压气量平衡报表',
//					index: 0,
//					icon: 'iconfont icon-wuxupailie',
//					link: '/wan/code/web/dataMonitorManage/hignReportList.html?pos=2&childPos=6'
//				}, {
//					title: 'LNG气量平衡报表',
//					index: 0,
//					icon: 'iconfont icon-wuxupailie',
//					link: '/wan/code/web/dataMonitorManage/lngReportList.html?pos=2&childPos=7'
//				}, {
//					title: '门站气量平衡报表',
//					index: 0,
//					icon: 'iconfont icon-wuxupailie',
//					link: '/wan/code/web/dataMonitorManage/stationReportList.html?pos=2&childPos=8'
//				}, {
//					title: '用户气量平衡报表',
//					index: 0,
//					icon: 'iconfont icon-wuxupailie',
//					link: '/wan/code/web/dataMonitorManage/userReportList.html?pos=2&childPos=9'
//				},
//				//				{
//				//					title: '平衡报表详情页',
//				//					index: 0,
//				//					icon: 'iconfont icon-wuxupailie',
//				//					link: '/wan/code/web/dataMonitorManage/detailsReportList.html?pos=2&childPos=10'
//				//				}
//			]
//		}, {
//			title: '合同管理',
//			index: 1,
//			icon: 'iconfont icon-qita',
//			link: '',
//			child: [{
//				title: '合同列表',
//				index: 0,
//				icon: 'layui-icon-layer',
//				link: '/wan/code/web/contractManage/contractList.html?pos=3&childPos=0'
//			}]
//		},
//		{
//			title: '气量管理',
//			index: 1,
//			icon: 'iconfont icon-xiangmuchuli',
//			link: '',
//			child: [{
//					title: '当日燃气价格',
//					index: 0,
//					icon: 'layui-icon layui-icon-dollar',
//					link: '/wan/code/web/airMaggessTem/arrPay.html?pos=4&childPos=0'
//				},
//				{
//					title: '合同日计划',
//					index: 0,
//					icon: 'layui-icon layui-icon-spread-left',
//					link: '/wan/code/web/airMaggessTem/contractDay.html?pos=4&childPos=1'
//				}, {
//					title: '结算查询',
//					index: 0,
//					icon: 'layui-icon layui-icon-survey',
//					link: '/wan/code/web/airMaggessTem/SettlementQuery.html?pos=4&childPos=2'
//				},
//				{
//					title: '交接通知（下游组织用户）',
//					index: 0,
//					icon: 'layui-icon layui-icon-notice',
//					link: '/wan/code/web/airMaggessTem/handoverNoticeUser.html?pos=4&childPos=3'
//				},
//				{
//					title: '合同日计划（下游组织用户）',
//					index: 0,
//					icon: 'layui-icon layui-icon-spread-left',
//					link: '/wan/code/web/airMaggessTem/contractDayUser.html?pos=4&childPos=4'
//				}, {
//					title: '燃气结算（下游组织用户）',
//					index: 0,
//					icon: 'layui-icon layui-icon-dollar',
//					link: '/wan/code/web/airMaggessTem/SettlementUserQuery.html?pos=4&childPos=5'
//				},
//			]
//		},
//		{
//			title: '账号管理',
//			index: 1,
//			icon: 'iconfont icon-jiaoseguanli',
//			link: '',
//			child: [{
//				title: '燃气公司管理',
//				index: 0,
//				icon: 'iconfont icon-banbenxinxi',
//				link: '/wan/code/web/adminManage/adminList.html?pos=5&childPos=0'
//			}, {
//				title: '下游组织管理',
//				index: 0,
//				icon: 'iconfont icon-gongzuokanban',
//				link: '/wan/code/web/adminManage/downstreamList.html?pos=5&childPos=1',
//			}, {
//				title: '用户管理',
//				index: 0,
//				icon: 'iconfont icon-gerenxinxi',
//				link: '/wan/code/web/adminManage/userList.html?pos=5&childPos=2',
//			}, {
//				title: '权限管理',
//				index: 0,
//				icon: 'iconfont icon-zuzhijiegou',
//				link: '/wan/code/web/adminManage/roleList.html?pos=5&childPos=3'
//			}]
//		}
	]
}
//提取菜单缓存数据
var isuser = window.sessionStorage.getItem("user");
isuser = JSON.parse(isuser);
if(isuser.menuList) {
	var menuList = isuser.menuList;
	var fristOther = [];
	var childNode = [];
	menuList.map(function(item) {
		if(item.parentId == 0) {
			fristOther.push({
				title: item.menuName,
				icon: item.menuLogoUrl,
				index: '1',
				link: '',
				id: item.id,
				parentId: item.parentId
			})
		} else {
			childNode.push({
				title: item.menuName,
				icon: item.menuLogoUrl,
				index: '0',
				link: item.menuUrl,
				id: item.id,
				parentId: item.parentId
			});
		}
	});
	fristOther.map(function(item) {
		var a = [];
		item.child = f(childNode, item, a);
	});

	function f(arr, item, a) {
		arr.map(function(list) {
			if(item.id == list.parentId) {
				a.push(list);
			}
		})
		return a
	};
}
var xiayou = {
	title: '气量管理',
	index: 1,
	icon: 'iconfont icon-xiangmuchuli',
	link: '',
	child: [{
			title: '交接通知',
			index: 0,
			icon: 'layui-icon layui-icon-notice',
			link: '/wan/code/web/airMaggessTem/handoverNoticeUser.html?pos=4&childPos=3'
		},
		{
			title: '合同日计划',
			index: 0,
			icon: 'layui-icon layui-icon-spread-left',
			link: '/wan/code/web/airMaggessTem/contractDayUser.html?pos=4&childPos=4'
		}, {
			title: '燃气结算',
			index: 0,
			icon: 'layui-icon layui-icon-dollar',
			link: '/wan/code/web/airMaggessTem/SettlementUserQuery.html?pos=4&childPos=5'
		},
	]
};
var admin = {
	title: '账号管理',
	index: 1,
	icon: 'iconfont icon-jiaoseguanli',
	link: '',
	child: [{
		title: '燃气公司管理',
		index: 0,
		icon: 'iconfont icon-banbenxinxi',
		link: '/wan/code/web/adminManage/adminList.html?pos=5&childPos=0'
	}]
};

if(isuser.msg.userType == 2) { //下游用户
	navData.leftNav.push(xiayou);
} else if(isuser.msg.userType == 1) { //燃气公司
	$.each(fristOther, function(i, item) {
		navData.leftNav.push(item);
	});
} else if(isuser.msg.userType == 0) { //admin甲方账户
	navData.leftNav.push(admin);
} else if(isuser.msg.userType == 3) { //用户
	$.each(fristOther, function(i, item) {
		navData.leftNav.push(item);
	});
}

function makeMenu(callback) {
	//	获取dom元素  用于存放生生成的列表
	var left = document.getElementById("left_menu");
	var str = '<ul class="layui-nav layui-nav-tree layui-bg-cyan layui-inline" lay-filter="demo">';
	if(left) {
		$.each(navData.leftNav, function(i, item) {
			var poss;
			if(item.index == 0) {
				poss = item.link.split("?")[1].split("&")[0].split("=")[1];
			} else {
				console.log(item)
				if(item.child[0]) {
					if(item.child[0].link.split("?")[1]) {
						poss = item.child[0].link.split("?")[1].split("&")[0].split("=")[1];
					} else {
						poss = "";
					}
				} else {
					item.link = item.link = "javascript:;";
				}
			};
			str += '<li class="layui-nav-item" datapos = "' + poss + '">';
			item.link == '' ? item.link = "javascript:;" : item.link;
			if(item.index == 0) {
				str += '<a href="' + item.link + '"><i class="layui-icon ' + item.icon + '"></i>' + item.title + '</a>';
			} else {
				str += '<a href="' + item.link + '"><i class="layui-icon ' + item.icon + '"></i>' + item.title + '<span class="layui-nav-more"></span></a><dl class="layui-nav-child">';
				$.each(item.child, function(j, child) {
					child.link == '' ? child.link = "javascript:;" : child.link;
					//					if(child.link == "")
					var pos;
					var childpos;
					if(child.link.split("?")[1]) {
						var pos = child.link.split("?")[1].split("&")[0].split("=")[1];
						var childpos = child.link.split("?")[1].split("&")[1].split("=")[1];
					} else {
						var childpos = "";
						var pos = "";
					}

					str += '<dd class="layONe" datapos="' + pos + '" datachildpos="' + childpos + '"><a href="' + child.link + '" class="nav-lay-a">' +
						'<i class="layui-icon ' + child.icon + '"></i>' + child.title + '</a>';
					if(child.index == 2) {
						str += '<dl class="layui-nav-child">';
						$.each(child.childs, function(k, twoChild) {
							console.log(twoChild)
							twoChild.link == '' ? twoChild.link = "javascript:;" : twoChild.link;
							str += '<dd class="layTwo" datapos="' + i + '" datachildpos="' + j + '"  datachildtow="' + k + '1' + twoChild.link + '" class="nav-lay-a twoA">' +
								'<i class="layui-icon ' + twoChild.icon + '"></i>' + twoChild.title + '</a></dd>';
						})
						str += '</dl>'
					}
					str += '</dd>';
				});
				str += '</dl>';
			}
			str += '</li>';
		});
		str += '</ul>';
		$(left).html(str);
		callback()
	} else {
		//		console.log("请初始化元素");
	}
}
makeMenu(function() {
	//	获取路径的值
	var href = window.location.href;
	href = href.split("?")[1];
	if(href.split("&")[1] && href.split("&")[1].split("=")[0] == "childPos") {
		//		console.log(1)
		//		判断是否还有子集元素
		if(href.split("&")[2] && href.split("&")[2].split("=")[0] == "childTwo") {
			var pos = href.split("&")[0].split("=")[1];
			var childPos = href.split("&")[1].split("=")[1];
			var childTwo = href.split("&")[2].split("=")[1];
			$.each($("#left_menu").find("li"), function(i, item) {
				if($(item).attr("datapos") == pos) {
					$(this).addClass("layui-nav-itemed");
					$.each($(item).find(".layONe"), function(j, child) {
						if($(child).attr("datachildpos") == childPos) {
							$(this).addClass("layui-nav-itemed")
							$.each($(child).find(".layTwo"), function(k, childTwo) {
								if($(childTwo).attr("datachildtow") == childTwo) {
									$(this).addClass("layui-this");
								}
							})
						}
					})
				}
			})
		} else {
			var pos = href.split("&")[0].split("=")[1];
			var childPos = href.split("&")[1].split("=")[1];
			$.each($("#left_menu").find("li"), function(i, item) {
				if($(item).attr("datapos") == pos) {
					$(this).addClass("layui-nav-itemed");
					$.each($(item).find(".layONe"), function(j, child) {
						if($(child).attr("datachildpos") == childPos) {
							$(this).addClass("layui-this")
						}
					})
				}
			})
		}
	} else {
		if(href.split("&")[0].split("=")[0] == "pos") {
			var pos = href.split("&")[0].split("=")[1];
			$.each($("#left_menu").find("li"), function(i, item) {
				if($(item).attr("datapos") == pos) {
					$(this).addClass("layui-this");
				}
			})
		}
	}
	console.log(href);

});
layui.use('element', function() {
	var element = layui.element;
	//监听导航点击
	element.on('nav(demo)', function(elem) {
		console.log(elem);
	});
});

function navF() {
	return navData
}